package com.aabte.cases;

import com.aabte.config.TestConfig;
import com.aabte.model.UpdateUserInfoCase;
import com.aabte.model.User;
import com.aabte.utils.DatabaseUtil;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
import org.apache.ibatis.session.SqlSession;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.IOException;

public class UpdateUserInfoTest {

    @Test(dependsOnGroups = "loginTrue", description = "更新用户ID为1的用户信息")
    public void updateUserInfo() throws IOException, InterruptedException {
        SqlSession session = DatabaseUtil.getSqlSession();
        UpdateUserInfoCase updateUserInfoCase = session.selectOne("updateUserInfoCase", 1);
        System.out.println(updateUserInfoCase);
        System.out.println(TestConfig.updateUserInfoUrl);

        int result = getResult(updateUserInfoCase);
        Thread.sleep(3000);
        User user = session.selectOne(updateUserInfoCase.getExpected(), updateUserInfoCase.getUserId());

        Assert.assertNotNull(user);
        Assert.assertEquals(1, result);
        // if (null != updateUserInfoCase.getUsername() && !"".equals(updateUserInfoCase.getUsername())) {
        //     Assert.assertEquals(user.getUsername(), updateUserInfoCase.getUsername());
        // }
        // if (null != updateUserInfoCase.getAge() && !"".equals(updateUserInfoCase.getAge())) {
        //     Assert.assertEquals(user.getAge(), updateUserInfoCase.getAge());
        // }
        // if (null != updateUserInfoCase.getSex() && !"".equals(updateUserInfoCase.getSex())) {
        //     Assert.assertEquals(user.getSex(), updateUserInfoCase.getSex());
        // }
        // if (null != updateUserInfoCase.getPermission() && !"".equals(updateUserInfoCase.getPermission())) {
        //     Assert.assertEquals(user.getPermission(), updateUserInfoCase.getPermission());
        // }
        // if (null != updateUserInfoCase.getIsDelete() && !"".equals(updateUserInfoCase.getIsDelete())) {
        //     Assert.assertEquals(user.getIsDelete(), updateUserInfoCase.getIsDelete());
        // }

    }

    @Test(dependsOnGroups = "loginTrue", description = "更新用户ID为1的用户信息")
    public void deleteUser() throws IOException, InterruptedException {
        SqlSession session = DatabaseUtil.getSqlSession();
        UpdateUserInfoCase deleteUserCase = session.selectOne("updateUserInfoCase", 2);
        System.out.println(deleteUserCase);
        System.out.println(TestConfig.updateUserInfoUrl);

        int result = getResult(deleteUserCase);
        Thread.sleep(3000);
        User user = session.selectOne(deleteUserCase.getExpected(), deleteUserCase.getUserId());

        Assert.assertNotNull(user);
        Assert.assertEquals(1, result);
        // Assert.assertEquals(user.getIsDelete(), "1");

    }

    private int getResult(UpdateUserInfoCase updateUserInfoCase) throws IOException {

        HttpPost post = new HttpPost(TestConfig.updateUserInfoUrl);
        JSONObject param = new JSONObject();
        param.put("id", updateUserInfoCase.getUserId());
        param.put("username", updateUserInfoCase.getUsername());
        param.put("sex", updateUserInfoCase.getSex());
        param.put("age", updateUserInfoCase.getAge());
        param.put("permission", updateUserInfoCase.getPermission());
        param.put("isDelete", updateUserInfoCase.getIsDelete());

        StringEntity entity = new StringEntity(param.toString(), "utf-8");
        post.setEntity(entity);

        post.setHeader("Content-type", "application/json");

        HttpResponse response = TestConfig.httpClient.execute(post);

        String result = EntityUtils.toString(response.getEntity());

        return Integer.parseInt(result);
    }



}
